﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Collections.Specialized;
using SqEngine.AI;
using sqengine.Components;


namespace SEEditor.Extended
{
    public partial class frmConfig : frmMDI
    {
        SaveManager _saveman;
        OpenFileDialog OpDialog;
        SaveFileDialog SvDialog;

        public frmConfig(SaveManager saveman, SaveFileDialog SDialog, OpenFileDialog ODialog)
        {
            InitializeComponent();
            _saveman = saveman;

            OpDialog = ODialog;
            SvDialog = SDialog;

            AntialiasingFillCombo();
            ResolutionFillCombo();
            this.Text = "Configuration editor";


        }

        private void frmConfig_Load(object sender, EventArgs e)
        {

            cbIntroEnabled_CheckedChanged(sender, e);
            cbAntialiasingEnabled_CheckedChanged(sender, e);

        }

        private void bntLoad_Click(object sender, EventArgs e)
        {
            OpDialog.Title = "Select Square config file";
            OpDialog.ShowDialog();
            if (OpDialog.FileName != "")
                LoadConfig(OpDialog.FileName);
        }

        private void AntialiasingFillCombo()
        {
            cbAntialiasing.Items.Clear();

            foreach (MultiSampleType item in Enum.GetValues(typeof(MultiSampleType)))
            {
                cbAntialiasing.Items.Add(item);
            }

        }

        private void ResolutionFillCombo()
        {
            cbResolution.Items.Clear();
            cbResolution.Items.Add("640x480");
            cbResolution.Items.Add("800x600");
            cbResolution.Items.Add("1024x768");
            cbResolution.Items.Add("1280x1024");
            cbResolution.Items.Add("1280x1080");
            cbResolution.Items.Add("1920x1080");
        }

        private void LoadConfig(string filename)
        {
            try
            {
                CfgSave cfg = _saveman.LoadConfig(filename);
                cbResolution.SelectedItem = cfg.ResolutionWidth + "x" + cfg.ResolutionHeight;
                cbFullscreen.Checked = cfg.Fullscreen;
                cbAntialiasing.SelectedItem = cfg.AntialiasingType;
                cbAntialiasingEnabled.Checked = cfg.AntialiasingEnabled;

                edtPlayerName.Text = cfg.PlayerName;
                edtIntro.Text = cfg.IntroFile;
                cbIntroEnabled.Checked = cfg.IntroEnabled;
                cbBloom.Checked = cfg.BloomEnabled;
                cbConsole.Checked = cfg.ConsoleEnabled;
                cbDebug.Checked = cfg.DebugMode;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, this.Text, MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }

        }

        private void SaveConfig(string filename)
        {
            try
            {

                CfgSave cfg = new CfgSave();

                cfg.ResolutionWidth = int.Parse(cbResolution.SelectedItem.ToString().Split('x')[0]);
                cfg.ResolutionHeight = int.Parse(cbResolution.SelectedItem.ToString().Split('x')[1]);
                cfg.Fullscreen = cbFullscreen.Checked;
                cfg.AntialiasingEnabled =  cbAntialiasingEnabled.Checked;
                cfg.AntialiasingType = (MultiSampleType)(cbAntialiasing.SelectedItem);
                cfg.PlayerName = edtPlayerName.Text;
                cfg.IntroEnabled = cbIntroEnabled.Checked;
                cfg.IntroFile = edtIntro.Text;
                cfg.BloomEnabled = cbBloom.Checked;
                cfg.ConsoleEnabled = cbConsole.Checked;
                cfg.DebugMode = cbDebug.Checked;

                _saveman.SaveConfig(filename, cfg);
                MessageBox.Show("Config saved!", this.Text, MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, this.Text, MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

            }

        }

        private void cbAntialiasingEnabled_CheckedChanged(object sender, EventArgs e)
        {
            cbAntialiasing.Enabled = cbAntialiasingEnabled.Checked;
        }



        private void cbIntroEnabled_CheckedChanged(object sender, EventArgs e)
        {
            edtIntro.Enabled = cbIntroEnabled.Checked;
        }



        private void bntNew_Click(object sender, EventArgs e)
        {

            edtIntro.Text = "";
            edtPlayerName.Text = "";            
            AntialiasingFillCombo();
            ResolutionFillCombo();

        }

        private void bntSave_Click(object sender, EventArgs e)
        {

            SvDialog.Title = "Choose path to save config";
            SvDialog.ShowDialog();
            SaveConfig(SvDialog.FileName);

        }



    }
}
